<template>
  <div class="login">
    <h1>{{ msg }}</h1>
    <form @submit.prevent="login" method="post">
      <input type="text" v-model="user.username" name="" id="" />
      <input type="text" v-model="user.password" name="" id="" />
      <button>登录</button>
    </form>
  </div>
</template>
<script>
import axios from "axios";
export default {
  data() {
    return {
      user: {
        username: "",
        password: "",
      },
      msg: "登录",
    };
  },
  methods: {
    async login() {
      await axios
        .post("http://127.0.0.1:7001/login", {
          user: this.user,
        })
        .then((res) => {
          if (res.data.code == 200) {
            localStorage.setItem("token", res.data.token);
            this.$router.push("/");
            console.log(res.data.token);
          } else {
            return (this.msg = "登陆失败");
          }
        });
    },
  },
};
</script>